sequenceDiagram
    title 服务购买-生成订单

    autonumber

    actor User as 用户
    participant DealerServicePurchaseOrderApplicationService

    participant DealerRepository
    participant DealerServicePurchaseOrderRepository
    participant DealerServicePurchaseOrderFactory
    participant Dealer
    participant DealerServicePurchaseOrder
    participant DomainEventPublisher


    User->>DealerServicePurchaseOrderApplicationService: purchaseService()

    activate DealerServicePurchaseOrderApplicationService

    DealerServicePurchaseOrderApplicationService->>DealerRepository: findById(DealerId) 获取 Dealer
    activate DealerRepository

    DealerRepository-->>DealerServicePurchaseOrderApplicationService: Dealer
    deactivate DealerRepository


    DealerServicePurchaseOrderApplicationService->>DealerServicePurchaseOrderRepository: existsInProcessing(DealerId) 是否存在进行中服务单
    activate DealerServicePurchaseOrderRepository
DealerServicePurchaseOrderRepository-->>DealerServicePurchaseOrderApplicationService: 是否存在进行中服务单
    deactivate DealerServicePurchaseOrderRepository

    alt 存在进行中服务单
        DealerServicePurchaseOrderApplicationService->>DealerServicePurchaseOrderApplicationService: 抛出异常中断流程
    else 不存在进行中服务单


        DealerServicePurchaseOrderApplicationService->>DealerServicePurchaseOrderFactory: createServicePurchaseOrder(Dealer) 创建 DealerServicePurchaseOrder
        activate DealerServicePurchaseOrderFactory

        DealerServicePurchaseOrderFactory->>Dealer: nextServicePeriod()

        activate Dealer
        Dealer-->>DealerServicePurchaseOrderFactory: 下一个服务周期
        deactivate Dealer

        DealerServicePurchaseOrderFactory->>DealerServicePurchaseOrder: 创建 DealerServicePurchaseOrder
        activate DealerServicePurchaseOrder
        DealerServicePurchaseOrder->>DealerServicePurchaseOrder: new DealerServicePurchaseOrder(...)
        DealerServicePurchaseOrder->>DomainEventPublisher: 发布领域事件 OrderCreated
        DealerServicePurchaseOrder-->>DealerServicePurchaseOrderFactory: DealerServicePurchaseOrder
        deactivate DealerServicePurchaseOrder

        DealerServicePurchaseOrderFactory-->>DealerServicePurchaseOrderApplicationService: DealerServicePurchaseOrder

        deactivate DealerServicePurchaseOrderFactory

        DealerServicePurchaseOrderApplicationService->>DealerServicePurchaseOrderRepository: save(DealerServicePurchaseOrder)

        DealerServicePurchaseOrderApplicationService-->>User: 订单 ID
    end


    deactivate DealerServicePurchaseOrderApplicationService